﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Xml.Serialization;
using Commander.Classes;
using Commander.Views;
using Commander.program;
using System.ComponentModel;
using System.Configuration;
using System.Threading.Tasks;
using Commander.Structures;

namespace Commander
{
    /// <summary>
    /// Основной клас файлового менеджера
    /// </summary>
    public partial class MainWindow : Window
    {
        private string PatchL;
        private string PatchR;

        //надо перенести все xml в мои документи
        private string loading = "Load.xml";

        private Point startPoint;

        FileSystemWatcher watc1 = new FileSystemWatcher();
        FileSystemWatcher watc2 = new FileSystemWatcher();

        private Edit editors;
        private bool _hidden;

        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// розпочати наблюдение за файловой системой
        /// </summary>
        /// <param name="sw"></param>
        /// <param name="str"></param>
        private void addwatch(FileSystemWatcher sw, string str)
        {
            sw.Path = str;
            sw.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            sw.Created += new FileSystemEventHandler(OnChanged);
            sw.Deleted += new FileSystemEventHandler(OnChanged);
            sw.Renamed += new RenamedEventHandler(watcher_Renamed);
            sw.EnableRaisingEvents = true;
        }

        /// <summary>
        /// удалить наблюдение за файловой системой
        /// </summary>
        /// <param name="sw"></param>
        private void delwatch(FileSystemWatcher sw)
        {
            sw.Created -= new FileSystemEventHandler(OnChanged);
            sw.Deleted -= new FileSystemEventHandler(OnChanged);
            sw.Renamed -= new RenamedEventHandler(watcher_Renamed);
            sw.EnableRaisingEvents = false;
        }

        void watcher_Renamed(object sender, RenamedEventArgs e)
        {
            //this.Dispatcher.BeginInvoke(new WatchDelegate(reloads), System.Windows.Threading.DispatcherPriority.Background);
            this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    string patch = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length);
                    if (patch == PatchL)
                    {
                        ViewsParam.InitsList(listView1, PatchL, label3, _hidden);
                    }
                    if (patch == PatchR)
                    {
                        ViewsParam.InitsList(listView2, PatchR, label4, _hidden);
                    }
                }));
        }


        //надо зделать сохранение виделених итемов после обновления списка
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            //this.Dispatcher.BeginInvoke(new WatchDelegate(reloads), System.Windows.Threading.DispatcherPriority.Background);
            this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    string patch = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length);
                    if (patch == PatchL)  
                    {
                        //ViewsParam.InitsList(listView1, PatchL, label3, _hidden);
                        if (e.ChangeType == WatcherChangeTypes.Created)
                        {
                            var tmp = listView1.SelectedItems;
                            ViewsParam.InitsList(listView1, PatchL, label3, _hidden);
                            foreach (var item in tmp)
                            {
                                listView1.SelectedItems.Add(item);
                            }
                            //ViewsParam.AddItem(listView1, e.FullPath,_hidden);
                        }
                        else if (e.ChangeType == WatcherChangeTypes.Deleted)
                        {
                            if (!Directory.Exists(PatchR))
                            {
                                string str;
                                int a=PatchR.IndexOf(@"\");
                                str=PatchR.Substring(0,a+1);
                                PatchR = str;
                                ViewPatc(wrapPanel4,PatchR);
                                delwatch(watc2);
                                addwatch(watc2, PatchR);
                                ViewsParam.InitsList(listView2, PatchR, label4, _hidden);
                            }
                            ViewsParam.InitsList(listView1, PatchL, label3, _hidden);
                            //ViewsParam.DelItem(listView1, e.FullPath);
                            
                        }
                    }
                    if (patch == PatchR)
                    {
                        if (e.ChangeType == WatcherChangeTypes.Created)
                        {
                            //ViewsParam.AddItem(listView2, e.FullPath, _hidden);
                            ViewsParam.InitsList(listView2, PatchR, label4, _hidden);
                        }
                        else if (e.ChangeType == WatcherChangeTypes.Deleted)
                        {
                            if (!Directory.Exists(PatchL))
                            {
                                string str;
                                int a = PatchR.IndexOf(@"\");
                                str = PatchR.Substring(0, a + 1);
                                PatchL = str;
                                ViewPatc(wrapPanel3, PatchL);
                                delwatch(watc1);
                                addwatch(watc1, PatchL);
                                ViewsParam.InitsList(listView1, PatchL, label3, _hidden);
                            }
                            ViewsParam.InitsList(listView2, PatchR, label4, _hidden);
                        }
                        //ViewsParam.InitsList(listView2, PatchR, label4, _hidden);
                    }
                }));
        }

        /// <summary>
        /// Настройка преложения при запуске
        /// </summary>
        private void Loading()
        {
            #region загрузка путей
            OpenPatch op = new OpenPatch(loading);
            PatchL = op.PatchL;
            PatchR = op.PatchR;
            #endregion

            InitUI();

            #region установка началних значений для UI
            ViewsParam.InitsList(listView1, PatchL, label3,_hidden);
            ViewsParam.InitsList(listView2, PatchR, label4, _hidden);
            ViewsParam.InitInfo(PatchL, label5);
            ViewsParam.InitInfo(PatchR, label6);

            addwatch(watc1, PatchL);
            addwatch(watc2, PatchR);

            ViewDsc(wrapPanel1);
            ViewDsc(wrapPanel2);

            ViewPatc(wrapPanel3, PatchL);
            ViewPatc(wrapPanel4, PatchR);

            editors = new Edit();

            Keyboard.Focus(listView1);
            #endregion
        }

        /// <summary>
        /// инициализация UI 
        /// </summary>
        private void InitUI()
        {
            Configured conf = Configured.Instance;
            if (!conf.StatusBar)
            {
                statusBar1.Visibility = Visibility.Hidden;
            }
            _hidden = conf.HiddenFolder;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Loading();
        }

        private void ViewPatc(WrapPanel wp, string Patch)
        {
            wp.Children.Clear();
            string tmp = System.IO.Path.GetPathRoot(Patch);
            string[] str = Patch.Split('\\');
            Button[] button = new Button[str.Count()];

            for (int a = 0; a < str.Count() - 1; a++)
            {
                string tooltipText = "";
                for (int r = 0; r <= a; r++)
                {
                    tooltipText += str[r] + "\\";
                }
                button[a] = new Button(); 
                button[a].Content = str[a] + "\\";
                //button[a].Click += new RoutedEventHandler(Patc_Click);
                button[a].Name = wp.Name;

                ToolTip tooltip = new System.Windows.Controls.ToolTip();
                tooltip.Content = tooltipText;
                button[a].ToolTip = tooltip;

                wp.Children.Add(button[a]);
            }
        }

        //отображение дисков в виде кнопок
        private void ViewDsc(WrapPanel wp)
        {
            wp.Children.Clear();
            int r = 0;
            DriveInfo[] allDrivers = DriveInfo.GetDrives();
            int a = allDrivers.Count();
            Button[] button = new Button[a+1];
            foreach (DriveInfo drive in allDrivers)
            {
                string tooltipmessage = "";
                if (drive.IsReady)
                {
                    tooltipmessage = "Метка тома: " + drive.VolumeLabel.ToString();
                    tooltipmessage += "\r\nФайловая система: " + drive.DriveFormat.ToString();
                    tooltipmessage += "\r\nСвободно " + Sizes.GetSize(drive.TotalFreeSpace) + " из " + Sizes.GetSize(drive.TotalSize);
                }
                else
                {
                    tooltipmessage = drive.DriveType.ToString();
                }
                int t = drive.Name.IndexOf(@"\");
                string str = drive.Name.Substring(0, t - 1);
                button[r] = new Button();
                button[r].Name = wp.Name + "_" + str;
                button[r].Content = drive.Name;
                button[r].Width = 25;
                button[r].Height = 25;
                button[r].Click += new RoutedEventHandler(MainWindow_Click);

                ToolTip tooltip = new ToolTip();
                tooltip.Content = tooltipmessage;
                button[r].ToolTip = tooltip;

                wp.Children.Add(button[r]);
                r++;
            }
            button[r] = new Button();
            button[r].Name = wp.Name + "_sety";
            button[r].Content = "сеть";
            button[r].Width = 35;
            button[r].Height = 25;
            button[r].Click+=new RoutedEventHandler(MainWindow_Click);

            ToolTip tooltip1 = new System.Windows.Controls.ToolTip();
            tooltip1.Content = "Робота с сетью(покамисть нереализовано)";
            button[r].ToolTip = tooltip1;

            wp.Children.Add(button[r]);
        }

        //void Patc_Click(object sender, RoutedEventArgs e)
        //{
        //    FrameworkElement feSource = e.Source as FrameworkElement;

        //    string q = feSource.Name;
        //    dynamic a = feSource.ToolTip;

        //    if (q == "wrapPanel3")
        //    {                
        //        PatchL = a.Content;
        //        ViewPatch(listView1, wrapPanel3, PatchL, label3, label5, watc1);
        //    }
        //    else if (q == "wrapPanel4")
        //    {
        //        PatchR = a.Content;
        //        ViewPatch(listView2, wrapPanel4, PatchR, label4, label6, watc2);
        //    }
        //}

        void MainWindow_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement feSource = e.Source as FrameworkElement;

            int a = feSource.Name.IndexOf("_");
            int b = feSource.Name.Length - a - 1;
            string q = feSource.Name.Substring(0, a);
            string disc = feSource.Name.Substring(a + 1, b) + @":\";
            if (q == "wrapPanel1")
            {
                DriveInfo[] allDrivers = DriveInfo.GetDrives();
                foreach (DriveInfo drive in allDrivers)
                {
                    if (drive.Name == disc)
                    {
                        if (drive.IsReady)
                        {
                            PatchL = disc;
                            ViewPatch(listView1, wrapPanel3, PatchL, label3, label5, watc1);
                        }
                        else
                        {
                            OpenDisk opd = new OpenDisk();
                            opd.ShowInTaskbar = false;
                            opd.Patch = PatchL;
                            opd.ShowDialog();
                            PatchL = opd.Patch;
                            ViewPatch(listView1, wrapPanel3, PatchL, label3, label5, watc1);
                        }
                    }
                }
            }
            else if (q == "wrapPanel2")
            {
                DriveInfo[] allDrivers = DriveInfo.GetDrives();
                foreach (DriveInfo drive in allDrivers)
                {
                    if (drive.Name == disc)
                    {
                        if (drive.IsReady)
                        {
                            PatchR = disc;
                            ViewPatch(listView2, wrapPanel4, PatchR, label4, label6, watc2);
                        }
                        else
                        {
                            OpenDisk opd = new OpenDisk();
                            opd.ShowInTaskbar = false;
                            opd.Patch = PatchR;
                            opd.ShowDialog();
                            PatchR = opd.Patch;
                            ViewPatch(listView2, wrapPanel4, PatchR, label4, label6, watc2);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Отображение файлов и папок по заданом пути в listview и некоторой дополнительной информации
        /// </summary>
        /// <param name="lw"></param>
        /// <param name="wp"></param>
        /// <param name="patch"></param>
        /// <param name="lb1"></param>
        /// <param name="lb2"></param>
        /// <param name="sw"></param>
        private void ViewPatch(ListView lw, WrapPanel wp, string patch, Label lb1, Label lb2, FileSystemWatcher sw)
        {
            ViewsParam.InitsList(lw, patch, lb1, _hidden);
            ViewsParam.InitInfo(patch, lb2);
            ViewPatc(wp, patch);
            delwatch(sw);
            addwatch(sw, patch);
            Keyboard.Focus(lw);
        }

        
        private void moves(IOView iv,ListView lw,ref string patch,Label lb2,WrapPanel wp)
        {
            if (iv.Size == "<Папка>")
            {
                if (iv.Name != "..")
                {
                    patch += iv.Name + @"\";
                }
                else
                {
                    patch = patch.Substring(0, patch.Substring(0, patch.Length - 1).LastIndexOf(@"\")) + @"\";
                }
                if (!ViewsParam.InitsList(lw, patch, lb2, _hidden))
                {
                    patch = patch.Substring(0, patch.Substring(0, patch.Length - 1).LastIndexOf(@"\")) + @"\";
                }
                else
                {
                    ViewPatc(wp, patch);
                    if (lw == listView1)
                    {
                        delwatch(watc1);
                        addwatch(watc1, PatchL);                        
                    }
                    else
                    {
                        delwatch(watc2);
                        addwatch(watc2, PatchR);
                    }
                }
            }
            //else if()
            //{
            //}
            else
            {
                if (iv.Type != "")
                {
                    //Почемуто некоторие проги при запуске падают
                    try
                    {
                        Process.Start(patch + iv.Name + "." + iv.Type);
                    }
                    catch (Exception a)
                    {
                        MessageBox.Show(a.ToString());
                    }
                }
                else
                {

                }
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IOView ioView = listView1.SelectedItem as IOView;
            if (ioView != null)
            {
                moves(ioView, listView1, ref PatchL, label3, wrapPanel3);
            }
        }

        private void listView2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IOView ioView = listView2.SelectedItem as IOView;
            if (ioView != null)
            {
                moves(ioView, listView2, ref PatchR, label4, wrapPanel4);
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void listView1_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            dynamic tmp = listView1.SelectedValue;
            IOView ioView = listView1.SelectedItem as IOView;
            System.Collections.IList str = listView1.SelectedItems;
            
            if(e.Key==Key.F4 && Keyboard.Modifiers==ModifierKeys.Shift)
            {
                Work.Create(PatchL);
                return;
            }
            else if(e.Key == Key.Delete && Keyboard.Modifiers == ModifierKeys.Shift)
            {
                Work.Delete(new ListOfFiles(PatchL, str));
                return;
            }

            switch (e.Key)
            {
                case Key.F2:
                    Work.Modify(PatchL, ioView);
                    break;

                case Key.F3:
                    Work.View(PatchL, ioView);
                    break;

                case Key.F4:
                    Work.Edit(new ListOfFiles(PatchL,str));
                    break;

                case Key.F5:
                    Work.Copy(new Info(PatchL, PatchR, str));
                    break;

                case Key.F6:
                    Work.Move(new Info(PatchL, PatchR, str));
                    break;

                case Key.F7:
                    Work.CreateDir(PatchL);
                    break;

                case Key.Delete:
                    MessageBoxResult result = MessageBox.Show("Удалить ?", "q", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                    }
                    break;

                case Key.Enter:
                    moves(ioView, listView1, ref PatchL, label3,wrapPanel3);
                    break;

                case Key.Back:
                    if (PatchL.Substring(PatchL.Length - 2, 2) != @":\")
                    {
                        PatchL = PatchL.Substring(0, PatchL.Substring(0, PatchL.Length - 1).LastIndexOf(@"\")) + @"\";
                        ViewPatch(listView1, wrapPanel3, PatchL, label3, label5, watc1);
                    }
                    break;
            }
        }

        private void listView2_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            IOView ioView = listView2.SelectedItem as IOView;
            System.Collections.IList str = listView2.SelectedItems;

            if (e.Key == Key.F4 && Keyboard.Modifiers == ModifierKeys.Shift)
            {
                Work.Create(PatchR);
                return;
            }
            else if (e.Key == Key.Delete && Keyboard.Modifiers == ModifierKeys.Shift)
            {
                Work.Delete(new ListOfFiles(PatchR, str));
                return;
            }

            switch (e.Key)
            {
                case Key.F2:
                    Work.Modify(PatchR, ioView);
                    break;

                case Key.F3:
                    Work.View(PatchR, ioView);
                    break;
                     
                case Key.F4:
                    Work.Edit(new ListOfFiles(PatchR,str));
                    break;

                case Key.F5:
                    Work.Copy(new Info(PatchR, PatchL, str));
                    break;

                case Key.F6:
                    Work.Move(new Info(PatchR, PatchL, str));
                    break;

                case Key.F7:
                    Work.CreateDir(PatchR);
                    break;

                case Key.Delete:
                    MessageBoxResult result = MessageBox.Show("Удалить ?", "q", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                    }
                    break;

                case Key.Enter:
                    moves(ioView, listView2, ref PatchR, label4,wrapPanel4);
                    break;

                case Key.Back:
                    if (PatchR.Substring(PatchR.Length - 2, 2) != @":\")
                    {
                        PatchR = PatchR.Substring(0, PatchR.Substring(0, PatchR.Length - 1).LastIndexOf(@"\")) + @"\";
                        ViewPatch(listView2, wrapPanel4, PatchR, label4, label6, watc2);
                    }
                    break;
            }
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            Config config = new Config();
            config.ShowInTaskbar = false;
            config.ShowDialog();
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowInTaskbar = false;
            about.ShowDialog();
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            try
            {
                Process.Start("cmd");
            }
            catch (Exception a)
            {
                MessageBox.Show(a.ToString());
            }
        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            try
            {
                Process.Start("regedit");
            }
            catch (Exception a)
            {
                MessageBox.Show(a.ToString());
            }
        }

        private void MenuItem_Click_5(object sender, RoutedEventArgs e)
        {
            //try 
            //{
            //    Process.Start("Watcher.exe");
            //}
            //catch (Exception a)
            //{
            //    MessageBox.Show(a.ToString());
            //}
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                //MessageBox.Show(EditPatch.Text);
                string str = StatusPatch.Text;
                string[] a = EditPatch.Text.Split(' ');
                string arg = "";
                for (int s = 1; s < a.Count(); s++)
                {
                    arg += a[s] + " ";
                }
                try
                {
                    Process.Start(str + a[0], arg);
                }
                catch (Exception y)
                {
                    MessageBox.Show(y.ToString());
                }
                finally
                {
                    EditPatch.Text = "";
                }
            }
        }

        private void listView1_GotFocus(object sender, RoutedEventArgs e)
        {
            StatusPatch.Text = PatchL;
        }

        private void listView2_GotFocus(object sender, RoutedEventArgs e)
        {
            StatusPatch.Text = PatchR;
        }

        private void listView2_Drop(object sender, DragEventArgs e)
        {
        //    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        //    {
        //        string[] data = (string[])e.Data.GetData(DataFormats.FileDrop);
        //        string[] data1 = new string[data.Length];
        //        int r = data[0].LastIndexOf('\\');
        //        for (int t = 0; t < data.Length; t++)
        //        {
        //            data1[t] = data[t].Substring(r + 1);
        //        }
        //        List<IOView> str = new List<IOView>();
        //        foreach (var a in data1)
        //        {
        //            if (File.Exists(data[0].Substring(0, r) + "\\" + a))
        //            {
        //                int s = a.LastIndexOf('.');
        //                if (s == -1)
        //                {
        //                    str.Add(new IOView(a, "", ""));
        //                }
        //                else
        //                {
        //                    str.Add(new IOView(a.Substring(0, s), a.Substring(s + 1, a.Length - 1 - s), ""));
        //                }
        //            }
        //            if (Directory.Exists(data[0].Substring(0, r) + "\\" + a))
        //            {
        //                str.Add(new IOView(a, "", "<Папка>"));
        //            }
        //        }

        //        if (data[0].Substring(0, r + 1) != PatchR)
        //        {
        //            Copy copy = new Copy(data[0].Substring(0, r + 1), PatchR, str);
        //            copy.Show();
        //        }
        //    }
        }

        private void listView1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(null);
        }

        private void listView1_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            //Point mousePos = e.GetPosition(null);
            //Vector diff = startPoint - mousePos;

            //if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            //{
            //    dynamic str = listView1.SelectedItems;
            //    if (str.Count > 0)
            //    {
            //        string[] data = new string[str.Count];
            //        for (int a = 0; a < str.Count; a++)
            //        {
            //            if (str[a].Size == "<Папка>")
            //            {
            //                data[a] = PatchL + str[a].Name;
            //            }
            //            else
            //            {
            //                data[a] = PatchL + str[a].Name + "." + str[a].Type;
            //            }
            //        }
            //        DataObject dragData = new DataObject(DataFormats.FileDrop, data);
            //        DragDrop.DoDragDrop(listView1, dragData, DragDropEffects.Move);
            //    }
            //}
        }

        private void listView1_Drop(object sender, DragEventArgs e)
        {
        //    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        //    {
        //        string[] data = (string[])e.Data.GetData(DataFormats.FileDrop);
        //        string[] data1 = new string[data.Length];
        //        int r = data[0].LastIndexOf('\\');
        //        for (int t = 0; t < data.Length; t++)
        //        {
        //            data1[t] = data[t].Substring(r + 1);
        //        }
        //        List<IOView> str = new List<IOView>();
        //        foreach (var a in data1)
        //        {
        //            if (File.Exists(data[0].Substring(0, r) + "\\" + a))
        //            {
        //                int s = a.LastIndexOf('.');
        //                if (s == -1)
        //                {
        //                    str.Add(new IOView(a, "", ""));
        //                }
        //                else
        //                {
        //                    str.Add(new IOView(a.Substring(0, s), a.Substring(s + 1, a.Length - 1 - s), ""));
        //                }
        //            }
        //            if (Directory.Exists(data[0].Substring(0, r) + "\\" + a))
        //            {
        //                str.Add(new IOView(a, "", "<Папка>"));
        //            }
        //        }

        //        if (data[0].Substring(0, r + 1) != PatchL)
        //        {
        //            Copy copy = new Copy(data[0].Substring(0, r + 1), PatchL, str);
        //            //copy.SetValue(data[0].Substring(0, r + 1), PatchL, str);
        //            copy.Show();
        //        }
        //    }
        }

        private void listView2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(null);
        }

        private void listView2_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            //Point mousePos = e.GetPosition(null);
            //Vector diff = startPoint - mousePos;

            //if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            //{
            //    dynamic str = listView2.SelectedItems;
            //    if (str.Count > 0)
            //    {
            //        string[] data = new string[str.Count];
            //        for (int a = 0; a < str.Count; a++)
            //        {
            //            if (str[a].Size == "<Папка>")
            //            {
            //                data[a] = PatchR + str[a].Name;
            //            }
            //            else
            //            {
            //                data[a] = PatchR + str[a].Name + "." + str[a].Type;
            //            }
            //        }
            //        DataObject dragData = new DataObject(DataFormats.FileDrop, data);
            //        DragDrop.DoDragDrop(listView2, dragData, DragDropEffects.Move);
            //    }
            //}
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            LoadPatch lp = new LoadPatch();
            lp.PatchL = PatchL;
            lp.PatchR = PatchR;

            #region сохранить пути
            try
            {
                XmlSerializer sr = new XmlSerializer(lp.GetType());
                StreamWriter sw = new StreamWriter(loading, false, Encoding.UTF8);
                sr.Serialize(sw, lp);
                sw.Close();
            }
            catch (Exception a)
            {
                MessageBox.Show(a.ToString());
            }
            #endregion
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F1 && Keyboard.Modifiers == ModifierKeys.Control)
            {
                SelectDisc selectedDisc = new SelectDisc();
                selectedDisc.ShowInTaskbar = false;
                selectedDisc.ShowDialog();
                string str = selectedDisc.Patch;
                if (str != "null")
                {
                    PatchL = str;
                    ViewPatch(listView1, wrapPanel3, PatchL, label3, label5, watc1);
                }
            }
            else if (e.Key == Key.F2 && Keyboard.Modifiers == ModifierKeys.Control)
            {
                SelectDisc selectedDisc = new SelectDisc();
                selectedDisc.ShowInTaskbar = false;
                selectedDisc.ShowDialog();
                string str = selectedDisc.Patch;
                if (str != "null")
                {
                    PatchR = str;
                    ViewPatch(listView2, wrapPanel4, PatchR, label4, label6, watc2);
                }
            }
        }
    }
}
